﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace backend_wcf
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Service : IService
    {
        private User.Type userType;
        Database db;
        public Service()
        {
            db = new Database();
            if (db.isEmpty())
            {
                bootStrap();
            }
            userType = User.Type.guest;
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public string HelloWorld(){
            return "Hello World!!";
        }

        public DataSet TestDB()
        {
            return db.TestDB();
        }

        public void addData(string tableName, Dictionary<string, Object> values)
        {
            db.addData(tableName, values);
        }

        public void createColumn(string tableName, string columnName, string dbType)
        {
            db.createColumn(tableName, columnName, dbType);
        }

        public void createTable(string name)
        {
            db.createTable(name);
        }

        public DataSet getData(string tableName, string logic_expression)
        {
            return db.getData(tableName, logic_expression);
        }

        public List<string> getStructure(string tableName)
        {
            return db.getStructure(tableName);
        }

        public List<string> getTables()
        {
            return db.getTables();
        }

        public void deleteData(string tableName, int id)
        {
            db.deleteData(tableName,id);
        }
        public void bootStrap()
        {
            db.createTable("users");
            db.createColumn("users", "username", "varchar(50)");
            db.createColumn("users", "passwd_md5", "varchar(50)");
            db.createColumn("users", "usertype", "int");
            db.createTable("files");
            db.createColumn("files", "name", "varchar(100)");
            db.createColumn("files", "data", "blob");
            db.createColumn("files", "size", "bigint");
            (new User("admin", "admin", db)).addUser(User.Type.admin);
        }



        public bool addUser(string userName, string password, User.Type userType) {
            if (this.userType != User.Type.admin)
            {
                return false;
            }
            return (new User(userName, password, db)).addUser(userType);
        }

        public User.Type login(string userName, string password) {
            this.userType = (new User(userName, password, db)).checkUser();
            return this.userType;    
            //return User.Type.admin;
        }
        public User.Type logout()
        {
            return this.userType = User.Type.guest;
        }

        public void addFile(string name, byte[] data) {
            try
            {
                if (userType == User.Type.guest) return;
               
                Dictionary<string, object> file = new Dictionary<string, object>();
                file.Add("name", name);
                file.Add("size", data.Length);
                file.Add("data", data);
                db.addData("files", file);
                return;
            }
            catch (Exception e)
            {
                return;
            }
        }

        public byte[] getFile(uint id) {
            if (userType == User.Type.guest) return null;
            DataSet ds = db.getData("files", "id="+id);
            foreach (DataRow row in ds.Tables["files"].Rows)
            {
                return (byte[])row["data"];
            }
            return null; 
        }

        public List<DummyFile> viewFiles() {
            List<DummyFile> files = new List<DummyFile>();
            DataSet ds = db.getData("files", null);
            foreach (DataRow row in ds.Tables["files"].Rows)
            {
                DummyFile file = new DummyFile(row["name"].ToString(), Convert.ToUInt32(row["size"]), Convert.ToUInt32(row["id"]));
                files.Add(file);
            }
            return files;
        }

        public void deleteFile(uint id)
        {
            db.deleteData("files", Convert.ToInt32(id));
        }
    }
}
